import React, { Component } from 'react'
import axios from 'axios'
import { withRouter} from 'react-router-dom'
import {loadData} from '../../redux/user.redux'
import {connect} from 'react-redux'

@withRouter
@connect(
    null,
    {loadData}
)
export default class AuthRoute extends Component {
    componentDidMount(){
        const publicList = ['/login','/register'];
        const pathname = this.props.location.pathname
        console.log(~publicList.indexOf(pathname))
        if(~publicList.indexOf(pathname)){
            console.log('login')
            return null
        }

        axios.get('/user/info')
            .then(res => {
                if (res.status == 200) {
                    if(res.data.code == 0){
                        //有登陆信息
                        this.props.loadData(res.data.data)
                    }else{
                       this.props.history.push('/login')
                    }
                }
            })
    }

    render() {
        return null
    }
}
